package com.cskaoyan.wordmemorize.excel;

import com.cskaoyan.wordmemorize.common.constant.ResultCodeEnum;
import com.cskaoyan.wordmemorize.common.exception.ExcelParseException;
import com.cskaoyan.wordmemorize.dao.mapper.VocMapper;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class SingleChoiceExcelValidator extends AbstractTopicExcelValidator{

    @Resource
    ResourceLoader resourceLoader;

    @Resource
    VocMapper vocMapper;


    @PostConstruct
    public void init() {
        super.init(resourceLoader, vocMapper);
    }

    @Override
    public void doValidate(Integer rowNum, Map<Integer, String> data) {
        super.doValidate(rowNum, data);

        String str = data.get(5);
        int answerIndex = 0;
        try {
            answerIndex = Integer.parseInt(str);
            if (answerIndex < 1 || answerIndex > 4) {
                throw new ExcelParseException(ResultCodeEnum.EXCEL_SAVE_ERROR, rowNum, "正确答案不在选项中");
            }
        } catch (NumberFormatException e) {
            throw new ExcelParseException(ResultCodeEnum.EXCEL_SAVE_ERROR, rowNum, "正确答案索引不是数值");
        }

    }

    @Override
    public String[] getTopicValidateWords(Map<Integer, String> data) {
        String answerIndexStr = data.get(5);
        int index = Integer.parseInt(answerIndexStr);
        return new String[]{data.get(index)};
    }
}
